﻿




function SelectFromInit(selector) {

    $(selector + " div[data-item]").attr('tabindex', '0');

    $(selector + " div[data-item]").keypress(function (evt) {
        // space or enter key
        if (evt.charCode == 32 || evt.charCode == 13) {
            SelectFromChange(selector, $(evt.currentTarget));
        }
    });

    $(selector + " div[data-item]").click(function (evt) {
        $(selector).focus();
        SelectFromChange(selector, $(evt.currentTarget));
    });
}

function SelectFromChange(selector, $elem) {

    var isSelected = $elem.attr('selected');
    var isAll = $elem.attr('data-item') == 'all';

    if (isAll && !isSelected) {
        $(selector + " div[data-item!=all]").removeAttr('selected');
        $elem.attr('selected', 'selected');
    }

    if (!isAll) {
        $(selector + " div[data-item=all]").removeAttr('selected');
        if (isSelected)
            $elem.removeAttr('selected');
        else
            $elem.attr('selected', 'selected');
    }

    if ($(selector + " div[data-item!=all][selected=selected]").length == 0) {
        $(selector + " div[data-item=all]").attr('selected', 'selected');
    }

    LogView.View.RenderView();


}